﻿// Type: System.Boolean
// Assembly: mscorlib, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// Assembly location: c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\mscorlib.dll

using System.Runtime.InteropServices;

namespace System
{
  [ComVisible(true)]
  public struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
  {
    public static readonly string TrueString = "True";
    public static readonly string FalseString = "False";
    internal const int True = 1;
    internal const int False = 0;
    internal const string TrueLiteral = "True";
    internal const string FalseLiteral = "False";
    private bool m_value;

    static Boolean()
    {
    }

    public override int GetHashCode()
    {
      if (!this)
        return 0;
      else
        return 1;
    }

    public override string ToString()
    {
      if (!this)
        return "False";
      else
        return "True";
    }

    public string ToString(IFormatProvider provider)
    {
      if (!this)
        return "False";
      else
        return "True";
    }

    public override bool Equals(object obj)
    {
      if (!(obj is bool))
        return false;
      else
        return this == (bool) obj;
    }

    public bool Equals(bool obj)
    {
      return this == obj;
    }

    public int CompareTo(object obj)
    {
      if (obj == null)
        return 1;
      if (!(obj is bool))
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeBoolean"));
      if (this == (bool) obj)
        return 0;
      if (!this)
        return -1;
      else
        return 1;
    }

    public int CompareTo(bool value)
    {
      if (this == value)
        return 0;
      if (!this)
        return -1;
      else
        return 1;
    }

    public static bool Parse(string value)
    {
      if (value == null)
        throw new ArgumentNullException("value");
      bool result = false;
      if (!bool.TryParse(value, out result))
        throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));
      else
        return result;
    }

    public static bool TryParse(string value, out bool result)
    {
      result = false;
      if (value == null)
        return false;
      if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
      {
        result = true;
        return true;
      }
      else if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
      {
        result = false;
        return true;
      }
      else
      {
        value = bool.TrimWhiteSpaceAndNull(value);
        if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
        {
          result = true;
          return true;
        }
        else
        {
          if (!"False".Equals(value, StringComparison.OrdinalIgnoreCase))
            return false;
          result = false;
          return true;
        }
      }
    }

    private static string TrimWhiteSpaceAndNull(string value)
    {
      int startIndex = 0;
      int index = value.Length - 1;
      char ch = char.MinValue;
      while (startIndex < value.Length && (char.IsWhiteSpace(value[startIndex]) || (int) value[startIndex] == (int) ch))
        ++startIndex;
      while (index >= startIndex && (char.IsWhiteSpace(value[index]) || (int) value[index] == (int) ch))
        --index;
      return value.Substring(startIndex, index - startIndex + 1);
    }

    public TypeCode GetTypeCode()
    {
      return TypeCode.Boolean;
    }

    bool IConvertible.ToBoolean(IFormatProvider provider)
    {
      return this;
    }

    char IConvertible.ToChar(IFormatProvider provider)
    {
      throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", (object) "Boolean", (object) "Char"));
    }

    sbyte IConvertible.ToSByte(IFormatProvider provider)
    {
      return Convert.ToSByte(this);
    }

    byte IConvertible.ToByte(IFormatProvider provider)
    {
      return Convert.ToByte(this);
    }

    short IConvertible.ToInt16(IFormatProvider provider)
    {
      return Convert.ToInt16(this);
    }

    ushort IConvertible.ToUInt16(IFormatProvider provider)
    {
      return Convert.ToUInt16(this);
    }

    int IConvertible.ToInt32(IFormatProvider provider)
    {
      return Convert.ToInt32(this);
    }

    uint IConvertible.ToUInt32(IFormatProvider provider)
    {
      return Convert.ToUInt32(this);
    }

    long IConvertible.ToInt64(IFormatProvider provider)
    {
      return Convert.ToInt64(this);
    }

    ulong IConvertible.ToUInt64(IFormatProvider provider)
    {
      return Convert.ToUInt64(this);
    }

    float IConvertible.ToSingle(IFormatProvider provider)
    {
      return Convert.ToSingle(this);
    }

    double IConvertible.ToDouble(IFormatProvider provider)
    {
      return Convert.ToDouble(this);
    }

    Decimal IConvertible.ToDecimal(IFormatProvider provider)
    {
      return Convert.ToDecimal(this);
    }

    DateTime IConvertible.ToDateTime(IFormatProvider provider)
    {
      throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", (object) "Boolean", (object) "DateTime"));
    }

    object IConvertible.ToType(Type type, IFormatProvider provider)
    {
      return Convert.DefaultToType((IConvertible) (bool) (this ? 1 : 0), type, provider);
    }
  }
}
